package com.zmc.product.controller;


import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.product.infrastructure.entity.CommodityBrandEntity;
import com.zmc.product.infrastructure.bean.query.CommodityBrandQuery;
import com.zmc.product.infrastructure.service.CommodityBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 商品中心的品牌Controller
 * @author zmc
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private CommodityBrandService commodityBrandService;

    @Autowired
    private AuthTokenService authTokenService;

    /**
     * 查询商品中心的品牌列表
     */
    @PostMapping("/list")
    public R list(@RequestBody CommodityBrandQuery query) {
        PageUtils page = commodityBrandService.pageCommodityBrandList(query);
        return R.page(page);
    }

    /**
     * 导出商品中心的品牌列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityBrandQuery query) {
        List<CommodityBrandEntity> list = commodityBrandService.selectCommodityBrandList(query);
        ExcelUtil<CommodityBrandEntity> util = new ExcelUtil<>(CommodityBrandEntity.class);
        util.exportExcel(response, list, "商品中心的品牌数据");
    }

    /**
     * 获取商品中心的品牌详细信息
     */
    @PostMapping(value = "/info")
    public R getInfo(@RequestBody CommodityBrandEntity commodityBrand) {
        return R.ok(commodityBrandService.getById(commodityBrand.getId()));
    }

    /**
     * 新增商品中心的品牌
     */
    @PostMapping("/add")
    public R add(@RequestBody CommodityBrandEntity commodityBrand) {
        String username = authTokenService.getUserInfo().getUsername();
        commodityBrand.setUserId(authTokenService.getUserInfo().getUserId());
        commodityBrand.setCreateBy(username);
        commodityBrandService.save(commodityBrand);
        return R.ok();
    }

    /**
     * 修改商品中心的品牌
     */
    @PostMapping("/update")
    public R edit(@RequestBody CommodityBrandEntity commodityBrand) {
        String username = authTokenService.getUserInfo().getUsername();
        commodityBrand.setUpdateBy(username);
        commodityBrandService.updateById(commodityBrand);
        return R.ok();
    }

    /**
     * 删除商品中心的品牌
     */
    @PostMapping("/delete")
    public R remove(@RequestBody CommodityBrandEntity commodityBrand) {
        commodityBrandService.removeById(commodityBrand.getId());
        return R.ok();
    }
}
